﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class OrtuEdit : Form
    {
        string par_id = "";
        private Ortu parent = null;

        public OrtuEdit()
        {
            InitializeComponent();
        }

        public OrtuEdit(Ortu instance, string id, string name, string address, string job, string phone, string mobile, string email)
        {
            InitializeComponent();

            parent = instance;

            this.par_id = id;
            this.txtNama.Text = name;
            this.txtAlamat.Text = address;
            this.txtPekerjaan.Text = job;
            this.textPhone.Text = phone;
            this.textMobile.Text = mobile;
            this.textEmail.Text = email;
        }

        private void OrtuEdit_Load(object sender, EventArgs e)
        {

        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            DataTable dt = Database.getInstance().tes("select * from Parent");
            DataRow[] data = dt.Select("par_id='" + this.par_id + "'");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i].ItemArray[0].ToString() == data[0].ItemArray[0].ToString())
                {
                    dt.Rows[i]["par_id"] = int.Parse(this.par_id);
                    dt.Rows[i]["par_name"] = this.txtNama.Text;
                    dt.Rows[i]["par_address"] = this.txtAlamat.Text;
                    dt.Rows[i]["par_job"] = this.txtPekerjaan.Text;
                    dt.Rows[i]["par_phone"] = this.textPhone.Text;
                    dt.Rows[i]["par_mobile"] = this.textMobile.Text;
                    dt.Rows[i]["par_email"] = this.textEmail.Text;
                    break;
                }
            }

            Database.getInstance().updateDB(dt);

            int current_row = this.parent.dataGridView1.CurrentCell.RowIndex;

            this.parent.dataGridView1.Rows[current_row].Cells["par_name"].Value = this.txtNama.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["par_address"].Value = this.txtAlamat.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["par_job"].Value = this.txtPekerjaan.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["par_phone"].Value = this.textPhone.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["par_mobile"].Value = this.textMobile.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["par_email"].Value = this.textEmail.Text;
            this.parent.dataGridView1.Refresh();
            this.Close();
        }
    }
}
